package com.ruoyi.web.controller.operate;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysReply;
import com.ruoyi.system.service.ISysReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 回复 信息操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/operate/reply")
public class ReplyController extends BaseController
{
    @Autowired
    private ISysReplyService replyService;

    /**
     * 获取回复列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysReply reply)
    {
        startPage();
        List<SysReply> list = replyService.selectReplyList(reply);
        return getDataTable(list);
    }

    /**
     * 根据回复编号获取详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        return success(replyService.selectReplyById(id));
    }

    /**
     * 新增回复
     */
    @Log(title = "回复", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysReply reply)
    {
        LoginUser loginUser = getLoginUser();//获取当前登录信息
        if(loginUser != null){
            SysUser sysUser = loginUser.getUser();
            if(sysUser != null){
                String isRegister = sysUser.getIsRegister();//是否注册 1已注册，0未注册
                if(StringUtils.isNotEmpty(isRegister) && isRegister.equals("1")){//注册，则代表用户
                    reply.setReplyType("2");
                }else if(StringUtils.isNotEmpty(isRegister) && isRegister.equals("0")){ //未注册，代表后台
                    reply.setReplyType("1");
                }
                reply.setCreateBy(String.valueOf(sysUser.getUserId()));
            }

        }
        return toAjax(replyService.insertReply(reply));
    }

    /**
     * 修改回复
     */
    @Log(title = "回复", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysReply reply)
    {
        reply.setUpdateBy(String.valueOf(getUserId()));
        return toAjax(replyService.updateReply(reply));
    }

    /**
     * 删除回复
     */
    @Log(title = "回复", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(replyService.deleteReplyByIds(ids));
    }

}
